package org.lex.keytool.ui.task;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import org.lex.keytool.Swings;
import org.lex.keytool.core.service.KeyTools;
import org.lex.keytool.core.task.CreatePublicAndPrivateKeyTask;
import org.lex.keytool.ui.builder.FileChooserBuilder;
import org.lex.keytool.ui.common.DNamePane;

public class CreatePublicAndPrivateKeyTaskPane extends BaseTaskPane {
	public static void main(String[] args) {
		Swings.show(new CreatePublicAndPrivateKeyTaskPane());
	}

	private JButton openFileButton;
	private JComboBox keyAlogComboBox;
	private JComboBox signAlogComboBox;
	private JComboBox storeTypeComboBox;
	private JLabel aliasLabel;
	private JLabel keyPasswordLabel;
	private JLabel keyAlogLabel;
	private JLabel signAlogLabel;
	private JLabel keySizeLabel;
	private JLabel validityLabel;
	private JLabel storePasswordLabel;
	private JLabel storeTypeLabel;
	private JLabel storeFileLabel;
	private JPanel keyPane;
	private DNamePane dnamePane;
	private JPanel storePane;
	private JPasswordField keyPasswordField;
	private JPasswordField storePasswordField;
	private JSpinner keySizeSpinner;
	private JSpinner validitySpinner;
	private JTextField aliasTextField;
	private JTextField storeFileTextField;

	public CreatePublicAndPrivateKeyTaskPane() {
		initComponents();
	}

	@Override
	public void restoreDefault() {
	}

	@Override
	public void run() {
		CreatePublicAndPrivateKeyTask task = new CreatePublicAndPrivateKeyTask();
		task.setAlias(this.aliasTextField.getText());
		task.setKeyPassword(new String(this.keyPasswordField.getPassword()));
		task.setKeyAlgorithm(this.keyAlogComboBox.getSelectedItem().toString());
		task.setSignAlgorithm(this.signAlogComboBox.getSelectedItem().toString());
		task.setKeySize(this.keySizeSpinner.getValue().toString());
		task.setValidity(this.validitySpinner.getValue().toString());
		task.setStoreType(this.storeTypeComboBox.getSelectedItem().toString());
		task.setKeyStore(this.storeFileTextField.getText());
		task.setStorePassword(new String(this.storePasswordField.getPassword()));
		task.setDname(this.dnamePane.getDName());
		this.runTask(task);
	}

	private void initComponents() {
		setLayout(new GridBagLayout());
		GridBagConstraints gc = null;
		// key pane
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(this.getKeyPane(), gc);
		// dname pane
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 0);
		add(this.getDnamePane(), gc);
		// store pane
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.weightx = 10.0;
		add(this.getStorePane(), gc);
		// glue
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.fill = GridBagConstraints.BOTH;
		gc.weightx = 10.0;
		gc.weighty = 10.0;
		add(Box.createGlue(), gc);
	}

	private JPanel getKeyPane() {
		if (null == keyPane) {
			keyPane = new JPanel(new GridBagLayout());
			keyPane.setBorder(BorderFactory.createTitledBorder("Key"));

			GridBagConstraints gc = null;
			aliasLabel = new JLabel("Alias");
			gc = new GridBagConstraints();
			gc.gridx = 0;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(aliasLabel, gc);

			aliasTextField = new JTextField("mykey");
			gc = new GridBagConstraints();
			gc.gridx = 1;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(aliasTextField, gc);

			keyPasswordLabel = new JLabel("Key Password");
			gc = new GridBagConstraints();
			gc.gridx = 2;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(keyPasswordLabel, gc);

			keyPasswordField = new JPasswordField();
			gc = new GridBagConstraints();
			gc.gridx = 3;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			gc.insets = new Insets(0, 0, 5, 0);
			keyPane.add(keyPasswordField, gc);

			keyAlogLabel = new JLabel("Key Algorithm");
			gc = new GridBagConstraints();
			gc.gridx = 0;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(keyAlogLabel, gc);

			keyAlogComboBox = new JComboBox(KeyTools.getSupportKeyAlgorithms());
			gc = new GridBagConstraints();
			gc.gridx = 1;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(keyAlogComboBox, gc);

			signAlogLabel = new JLabel("Sign Algorithm");
			gc = new GridBagConstraints();
			gc.gridx = 2;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			keyPane.add(signAlogLabel, gc);

			signAlogComboBox = new JComboBox(KeyTools.getSupportSignAlgorithms());
			gc = new GridBagConstraints();
			gc.gridx = 3;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			gc.insets = new Insets(0, 0, 5, 0);
			keyPane.add(signAlogComboBox, gc);

			keySizeLabel = new JLabel("Key Size");
			gc = new GridBagConstraints();
			gc.gridx = 0;
			gc.gridy = 2;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 0, 5);
			keyPane.add(keySizeLabel, gc);

			keySizeSpinner = new JSpinner(new SpinnerNumberModel(1024, 1, Short.MAX_VALUE, 1));
			gc = new GridBagConstraints();
			gc.gridx = 1;
			gc.gridy = 2;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			gc.insets = new Insets(0, 0, 0, 5);
			keyPane.add(keySizeSpinner, gc);

			validityLabel = new JLabel("Validity");
			gc = new GridBagConstraints();
			gc.gridx = 2;
			gc.gridy = 2;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 0, 5);

			validitySpinner = new JSpinner(new SpinnerNumberModel(90, 1, Short.MAX_VALUE, 10));
			keyPane.add(validityLabel, gc);
			gc = new GridBagConstraints();
			gc.gridx = 3;
			gc.gridy = 2;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 10.0;
			keyPane.add(validitySpinner, gc);

		}
		return keyPane;
	}

	private DNamePane getDnamePane() {
		if (null == dnamePane) {
			dnamePane = new DNamePane();
			dnamePane.setBorder(BorderFactory.createTitledBorder("Distinguished Name"));
		}
		return dnamePane;
	}

	private JPanel getStorePane() {
		if (null == storePane) {
			storePane = new JPanel(new GridBagLayout());
			storePane.setBorder(BorderFactory.createTitledBorder("Store"));

			GridBagConstraints gc = null;

			storePasswordLabel = new JLabel("Store Password");
			gc = new GridBagConstraints();
			gc.gridx = 0;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			storePane.add(storePasswordLabel, gc);

			storePasswordField = new JPasswordField();
			gc = new GridBagConstraints();
			gc.gridx = 1;
			gc.gridy = 0;
			gc.weightx = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.insets = new Insets(0, 0, 5, 5);
			storePane.add(storePasswordField, gc);

			storeTypeLabel = new JLabel("Store Type");
			gc = new GridBagConstraints();
			gc.gridx = 2;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 5, 5);
			storePane.add(storeTypeLabel, gc);

			storeTypeComboBox = new JComboBox(KeyTools.getSupportKeyStoreTypes());
			gc = new GridBagConstraints();
			gc.gridx = 3;
			gc.gridy = 0;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			storePane.add(storeTypeComboBox, gc);

			storeFileLabel = new JLabel("Store File");
			gc = new GridBagConstraints();
			gc.gridx = 0;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.insets = new Insets(0, 0, 0, 5);
			storePane.add(storeFileLabel, gc);

			storeFileTextField = new JTextField();
			gc = new GridBagConstraints();
			gc.gridx = 1;
			gc.gridy = 1;
			gc.gridwidth = 2;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			gc.fill = GridBagConstraints.HORIZONTAL;
			gc.weightx = 1;
			gc.insets = new Insets(0, 0, 0, 5);
			storePane.add(storeFileTextField, gc);

			openFileButton = new JButton("Open...");
			gc = new GridBagConstraints();
			gc.gridx = 3;
			gc.gridy = 1;
			gc.anchor = GridBagConstraints.BASELINE_LEADING;
			storePane.add(openFileButton, gc);

			FileChooserBuilder.addHandler(openFileButton, storeFileTextField, false);
		}
		return storePane;
	}
}
